package com.qyb.barcode;


import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 实现功能：
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
public class ImageRenderListener2 implements RenderListener {
    int counter = 100000;
    List<byte[]> imageAsBytes = new ArrayList<>();
    public ImageRenderListener2() {
    }

    public void beginTextBlock() { }
    public void renderText(TextRenderInfo renderInfo) { }
    public void endTextBlock() { }

    public void renderImage(ImageRenderInfo renderInfo)
    {
        PdfImageObject image = null;
        try
        {
            image = renderInfo.getImage();
        }
        catch (IOException e)
        {
            image = null;
            e.printStackTrace();
        }
        finally {
            if (image == null) return;
//            int number = renderInfo.getRef() != null ? renderInfo.getRef().getNumber() : counter++;
            imageAsBytes.add(image.getImageAsBytes());
        }
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public List<byte[]> getImageAsBytes() {
        return imageAsBytes;
    }

    public void setImageAsBytes(List<byte[]> imageAsBytes) {
        this.imageAsBytes = imageAsBytes;
    }
}
